<!--

    Copyright (C) 2015 The Gravitee team (http://gravitee.io)

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

            http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<button mat-button aria-label="Go back to your subscriptions" [routerLink]="'../'">
  <mat-icon svgIcon="gio:nav-arrow-left"></mat-icon> Go back to subscriptions list
</button>

@if (pageVM$ | async; as pageVM) {
  <mat-card class="subscriptionDetailsCard">
    <mat-card-header>
      <mat-card-title>Subscription details</mat-card-title>
    </mat-card-header>
    <mat-card-content>
      <dl class="gio-description-list">
        <dt>ID</dt>
        <dd gioClipboardCopyWrapper [contentToCopy]="pageVM.subscription.id">
          {{ pageVM.subscription.id || '-' }}
        </dd>

        <dt>API</dt>
        <dd gioClipboardCopyWrapper [contentToCopy]="pageVM.subscription.api.name + ' - ' + pageVM.subscription.api.version">
          {{ pageVM.subscription.api.name }} - {{ pageVM.subscription.api.version }}
        </dd>

        <dt>Plan</dt>
        <dd gioClipboardCopyWrapper [contentToCopy]="pageVM.subscription.plan.name">
          {{ pageVM.subscription.plan.name || '-' }}
        </dd>

        <dt>Status</dt>
        <dd gioClipboardCopyWrapper [contentToCopy]="pageVM.subscription.status | uppercase">
          {{ (pageVM.subscription.status | uppercase) || '-' }}
        </dd>

        <dt>Subscribed by</dt>
        <dd gioClipboardCopyWrapper [contentToCopy]="pageVM.subscription.subscribed_by.displayName">
          {{ pageVM.subscription.subscribed_by.displayName || '-' }}
        </dd>

        @if (pageVM.subscription.request) {
          <dt>Publisher message to subscriber</dt>
          <dd gioClipboardCopyWrapper [contentToCopy]="pageVM.subscription.request">
            {{ pageVM.subscription.request || '-' }}
          </dd>
        }

        @if (pageVM.subscription.reason) {
          <dt>Subscriber message to publisher</dt>
          <dd gioClipboardCopyWrapper [contentToCopy]="pageVM.subscription.reason">
            {{ pageVM.subscription.reason || '-' }}
          </dd>
        }

        <dt>Created at</dt>
        <dd>{{ (pageVM.subscription.created_at | date: 'medium') || '-' }}</dd>
        <dt>Processed at</dt>
        <dd>{{ (pageVM.subscription.processed_at | date: 'medium') || '-' }}</dd>

        @if ('REJECTED' !== pageVM.subscription.status) {
          <dt>Starting at</dt>
          <dd>{{ (pageVM.subscription.starting_at | date: 'medium') || '-' }}</dd>
          <dt>Paused at</dt>
          <dd>{{ (pageVM.subscription.paused_at | date: 'medium') || '-' }}</dd>
          <dt>Ending at</dt>
          <dd>{{ (pageVM.subscription.ending_at | date: 'medium') || '-' }}</dd>
        }
        <dt>Closed at</dt>
        <dd>{{ (pageVM.subscription.closed_at | date: 'medium') || '-' }}</dd>
      </dl>
    </mat-card-content>

    @if (pageVM.subscription.status === 'ACCEPTED' || pageVM.subscription.status === 'PENDING' || pageVM.subscription.status === 'PAUSED') {
      <mat-card-actions *gioPermission="{ anyOf: ['application-subscription-d'] }">
        <button mat-raised-button color="warn" (click)="closeSubscription(pageVM.application, pageVM.subscription)">
          <mat-icon svgIcon="gio:x-circle"></mat-icon>
          Close
        </button>
      </mat-card-actions>
    }
  </mat-card>

  @if (pageVM.subscription.plan.security === 'API_KEY') {
    <subscription-api-keys
      [applicationId]="pageVM.application.id"
      [subscriptionId]="pageVM.subscription.id"
      [readonly]="pageVM.application.api_key_mode === 'SHARED' || pageVM.subscription.status !== 'ACCEPTED'"
      [subtitleText]="
        pageVM.application.api_key_mode === 'SHARED'
          ? 'This subscription uses a shared API Key. You can renew or revoke the shared API Key at the application level.'
          : undefined
      "
    >
      ></subscription-api-keys
    >
  }
} @else {
  <mat-card>
    <mat-card-header>
      <mat-card-title>Subscription details</mat-card-title>
    </mat-card-header>
    <mat-card-content> <gio-loader></gio-loader> </mat-card-content>
  </mat-card>
}
